/*
 *
 *  Copyright (C) 2012-2024
 *  All rights reserved, Designed By 杭州连线客信息技术有限公司
 *  注意：
 *  本软件为杭州连线客信息技术有限公司开发研制，未经购买不得使用
 *  购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 *  一经发现盗用、分享等行为，将追究法律责任，后果自负
 *
 */

package com.lxk.ums.api;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Opt;
import com.lxk.service.core.Result;
import com.lxk.service.ums.api.UmsServiceApi;
import com.lxk.service.ums.request.QueryUserAddressParam;
import com.lxk.service.ums.request.QueryUserParam;
import com.lxk.service.ums.response.UmsUserAddressResponse;
import com.lxk.service.ums.response.UmsUserDto;
import com.lxk.ums.domain.bo.UmsUserAddressBo;
import com.lxk.ums.domain.vo.UmsUserAddressVo;
import com.lxk.ums.domain.vo.UmsUserVo;
import com.lxk.ums.service.IUmsUserAddressService;
import com.lxk.ums.service.IUmsUserService;
import lombok.AllArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.ValidationUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户内部API
 *
 * @author heyi
 * 2024/8/20
 */
@Service
@AllArgsConstructor
public class UmsServiceApiImpl implements UmsServiceApi {
    private final IUmsUserService umsUserService;
    private final IUmsUserAddressService umsUserAddressService;

    @Override
    public Result<UmsUserDto> queryById(Long userId) {
        UmsUserVo umsUserVo = umsUserService.queryById(userId);
        UmsUserDto dto = MapstructUtils.convert(umsUserVo, UmsUserDto.class);
        return Result.ok(dto);
    }

    /**
     * 查询用户
     *
     * @param param
     * @return
     */
    @Override
    public Result<UmsUserDto> queryByParam(QueryUserParam param) {
        ValidationUtils.validate(param);
        Opt<UmsUserVo> umsUserVoOpt = umsUserService.queryByPhone(param.getPhone());
        UmsUserVo umsUserVo = umsUserVoOpt.orElse(new UmsUserVo());
        UmsUserDto umsUserDto = MapstructUtils.convert(umsUserVo, UmsUserDto.class);
        return Result.ok(umsUserDto);
    }

    /**
     * 查询ID集合
     *
     * @return
     */
    @Override
    public Result<List<UmsUserDto>> queryByIdList(List<Long> userIdList) {
        List<UmsUserVo> umsUserVoList = umsUserService.queryByIdList(userIdList);
        List<UmsUserDto> umsUserDtoList = MapstructUtils.convert(umsUserVoList, UmsUserDto.class);
        return Result.ok(umsUserDtoList);
    }

    @Override
    public Result<UmsUserAddressResponse> queryUserAddress(QueryUserAddressParam param) {
        ValidationUtils.validate(param);
        UmsUserAddressBo bo = new UmsUserAddressBo();
        bo.setId(param.getId());
        bo.setUserId(param.getUserId());
        List<UmsUserAddressVo> addressVos = umsUserAddressService.queryList(bo);
        if (CollUtil.isEmpty(addressVos)) {
            return Result.fail("未找到用户地址");
        }
        return Result.ok(MapstructUtils.convert(addressVos.get(0), UmsUserAddressResponse.class));
    }

    @Override
    public Result<UmsUserAddressResponse> queryDefaultAddress(Long userId) {
        UmsUserAddressVo vo = umsUserAddressService.getDefaultAddress(userId);
        return Result.ok(MapstructUtils.convert(vo, UmsUserAddressResponse.class));
    }
}
